//
//  RecommendTableView.m
//  EasyBuy
//
//  Created by Zhu Xiangjun on 10/4/12.
//  Copyright (c) 2012 shen donghao. All rights reserved.
//

#import "RecommendTableView.h"
#import "EasyBuyRequest.h"
#import "EasyBuyService.h"
#import "RecommendData.h"
#import "RecommendItemData.h"
#import "RecommendTableViewCell.h"
#import "BuyInfoTableViewController.h"
#import "EasyBuy.h"

@interface RecommendTableView(){
    NSMutableArray *_selectedSections;
}

@end

@implementation RecommendTableView

@synthesize recommendDataArray;

-(void)dealloc{
    [super dealloc];
    [_selectedSections release];
    [recommendDataArray release];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        _selectedSections = [[NSMutableArray alloc] init];        
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [self initRecommendViesData];
        
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
    }
    return self;
}

-(void) initRecommendViesData{
    
    
    EasyBuyService *service =  [[EasyBuyService alloc] init];
    EasyBuyRequest<RequestProtocol> *request = [[EasyBuyRequest alloc] initRecommendRequest];
    [service executeRecommendRequest:request :RECEIVER_HOME_RECOMMEND];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(onReceivedRecommendData:)
												 name:RECEIVER_HOME_RECOMMEND
											   object:nil];
    
}

-(void)onReceivedRecommendData:(NSNotification *)notification{
    
    NSArray *_recommendArray = notification.object;
    self.recommendDataArray = _recommendArray;
    [self reloadData];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


#pragma mark tableView delegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    RecommendData *recommendData = [self.recommendDataArray objectAtIndex:section];
    
    UIView* sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)] autorelease];
    sectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"subject_item_bg.png"]];
    sectionView.userInteractionEnabled = YES;
    
    UIImage *arrowImage = [UIImage imageNamed:@"section_arrowRight.png"];
    UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (sectionView.frame.size.height-arrowImage.size.height)/2, arrowImage.size.width, arrowImage.size.height)];
    [arrowImageView setImage:arrowImage];
    [sectionView addSubview:arrowImageView];
    [arrowImage release];
    
    UILabel *sectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 60, 30)];
    sectionLabel.backgroundColor = [UIColor clearColor];
    sectionLabel.userInteractionEnabled = YES;
    sectionLabel.textAlignment = UITextAlignmentCenter;
    sectionLabel.text = recommendData.recommendtitle;
    sectionLabel.font = [UIFont systemFontOfSize:14.0f];
    [sectionView addSubview:sectionLabel];
    [sectionLabel release];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = sectionView.bounds;
    button.tag = section; //记录所选section值
    [button addTarget:self action:@selector(sectionTaped:) forControlEvents:UIControlEventTouchUpInside];
    [sectionView addSubview:button];
    
    return sectionView;
}

- (void)sectionTaped:(UIButton *)sender
{
    NSInteger section = sender.tag;
    NSNumber *sectionNum = [NSNumber numberWithInteger:section];
    if ([_selectedSections containsObject:sectionNum]) {
        [_selectedSections removeObject:sectionNum];
    } else {
        [_selectedSections addObject:sectionNum];
    }
    
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];
    [self reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
}

#pragma mark tableView data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.recommendDataArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([_selectedSections containsObject:[NSNumber numberWithInteger:section]]) {
        return 0;
    }
    RecommendData *recommendData = [self.recommendDataArray objectAtIndex:section];

    return [recommendData.recommendItemsArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cellId";
    RecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[RecommendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    RecommendData *recommendData = [self.recommendDataArray objectAtIndex:indexPath.section];
    RecommendItemData *recommendItemData = [recommendData.recommendItemsArray objectAtIndex:indexPath.row];
    [cell initCellWithRecommendItemData:recommendItemData];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RecommendData *recommendData = [self.recommendDataArray objectAtIndex:indexPath.section];
    RecommendItemData *recommendItemData = [recommendData.recommendItemsArray objectAtIndex:indexPath.row];
    NSNumber *number = [NSNumber numberWithInt:recommendItemData.contentid];
    [[NSNotificationCenter defaultCenter] postNotificationName:RECEIVER_HOME_VIEWCONTROLLER object:number];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}



@end
